package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: NursingPlanController
 * @description:
 * @author: InnovatorX
 * @date: 2024/5/27 10:14
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/nursing")
public class NursingPlanController extends BaseController {
    @Autowired
    private NursingPlanService nursingPlanService;

    /**
     * 查询所有护理计划
     * @return
     */
    @GetMapping("/plan")
    public ResponseResult<List<NursingPlanVo>> findAllPlan(){
        List<NursingPlanVo> planVoList = nursingPlanService.findAllPlan();
        return success(planVoList);
    }

    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/plan/search")
    public ResponseResult findByPage(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "status",required = false) Integer status,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ){
     PageResponse<NursingPlanVo> pageResponse = nursingPlanService.findByPage(name,status,pageNum,pageSize);
        return success(pageResponse);
    }

    /**
     * 新增计划
     * @param dto
     * @return
     */
    @PostMapping("/plan")
    public ResponseResult savePlan(@RequestBody NursingPlanDto dto){
        // 一次操作两张表
        nursingPlanService.save(dto);
        return success();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/plan/{id}")
    public ResponseResult<NursingPlanVo> findById(@PathVariable("id")Long id){
        NursingPlanVo nursingPlan =  nursingPlanService.findById(id);
        return success(nursingPlan);
    }

    /**
     * 修改计划
     * @param dto
     * @return
     */
    @PutMapping("/plan/{id}")
    public ResponseResult updatePlan(@PathVariable(name = "id")Long id,
                                     @RequestBody NursingPlanDto dto){
        nursingPlanService.update(id,dto);
        return success();
    }

    /**
     * 删除计划
     * @param id
     * @return
     */
    @DeleteMapping("/plan/{id}")
    public ResponseResult deleteById(@PathVariable("id")Long id){
        nursingPlanService.deleteById(id);
        return success();
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    public ResponseResult updateBySta(@PathVariable("id")Long id,
                                      @PathVariable("status")Integer status){
        nursingPlanService.updateBySta(id,status);
        return success();
    }
}
